package com.api.shopapi.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.CheckinDTO;
import com.api.shopapi.entity.dto.CheckinDTO2;
import com.api.shopapi.mapper.CheckinMapper;
import com.api.shopapi.service.*;
import com.api.shopapi.util.DateUtil;
import com.api.shopapi.util.DoubleUtil;
import com.api.shopapi.util.Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author maochuan
 */
@Service("CheckinServiceImplOne")
public class CheckinServiceImpl implements CheckinService {

    @Autowired
    private CheckinMapper checkinMapper;

    @Autowired
    @Qualifier("ElderlyServiceImplOne")
    private ElderlyService elderlyService;


    @Autowired
    @Qualifier("CheckinSetmealServiceImplOne")
    private CheckinSetmealService checkinSetmealService;

    @Autowired
    @Qualifier("BerthServiceImplOne")
    private BerthService berthService;

    @Autowired
    @Qualifier("RoomServiceImplOne")
    private RoomService roomService;

    @Autowired
    @Qualifier("RoomTypeServiceImplOne")
    private RoomTypeService roomTypeService;


    @Autowired
    @Qualifier("RetreatSettleServiceImplOne")
    private RetreatSettleService retreatSettleService;


    @Override
    public int insert(Checkin checkin) {
        return checkinMapper.insert(checkin);
    }

    @Override
    public int update(Checkin checkin) {
        return checkinMapper.updateByCheckinCode(checkin);
    }


    @Override
    public Checkin getByCheckinCode(String checkinCode) {
        return checkinMapper.getByCheckinCode(checkinCode);
    }

    @Override
    public Checkin getByElderlyCodeAndInstitutionCode(String elderlyCode, String institutionCode) {
        return checkinMapper.getByElderlyCodeAndInstitutionCode(elderlyCode, institutionCode);
    }

    @Override
    public int delete(String checkinCode) {
        return checkinMapper.delete(checkinCode);
    }

    @Override
    public PageInfo<Checkin> getPageList(int pageIndex, int pageSize, String institutionCode, String elderlyName, int status, String startTime, String endTime) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Checkin> checkins = checkinMapper.getListByInstitutionCode(institutionCode, elderlyName, status, startTime, endTime);
        PageInfo<Checkin> pageInfo = new PageInfo<>(checkins);
        return pageInfo;
    }

    //套餐退住
    @Override
    @Transactional
    public int updateToStatus2(Checkin checkin, RetreatSettle retreatSettle, String elderlyCode, Berth berth, int isRefund) {
        retreatSettleService.insert(retreatSettle);
        //减少老人余额
        double SumMoney = DoubleUtil.add(retreatSettle.getTotal(), retreatSettle.getReturnBlance());
//        elderlyService.updateByJianBalance(elderlyCode, SumMoney);
//        //如果有退款添加退款记录
//        if (isRefund == 1) {
//            RefundRecord refundRecord = new RefundRecord();
//            refundRecord.setRefundRecordCode(UUID.randomUUID().toString());
//            refundRecord.setElderlyCode(elderlyCode);
//            refundRecord.setElderlyName(retreatSettle.getElderlyName());
//            refundRecord.setRefundType(1);
//            refundRecord.setReturnBalance(retreatSettle.getReturnBlance());
//            refundRecord.setCreateTime(new Date());
//            refundRecord.setRemarks("退住退款");
//            refundRecord.setStaffCode(retreatSettle.getStaffCode());
//            refundRecord.setInstitutionCode(retreatSettle.getInstitutionCode());
//            refundRecordService.insert1(refundRecord);
//        }

        berthService.updateByBerthCode(berth);

        return checkinMapper.updateByCheckinCode(checkin);
    }

    /**
     * 套餐转房结算
     *
     * @param checkin
     * @param jsonObject
     * @param moneyPlan
     * @param retreatSettle
     * @return
     */
    @Override
    @Transactional
    public int RoomChangeToElderly(Checkin checkin, JSONObject jsonObject, double moneyPlan, RetreatSettle retreatSettle) {
        String staffCode = jsonObject.getString("staffCode");
        String remarks = jsonObject.getString("remarks");
        String checkinSetmealCode = jsonObject.getString("checkinSetmealCode");
        String newBerthCode = jsonObject.getString("berthCode");
        //预计结束时间
        String endTimePlan = jsonObject.getString("endTimePlan");
        double price = 0;
        if(checkin.getChargeType()==2){
            price = jsonObject.getDouble("price");
        }
        JSONArray feeArr = jsonObject.getJSONArray("feeArr");

        String elderlyCode = checkin.getElderlyCode();
        String elderlyName = checkin.getElderlyName();
        String institutionCode = checkin.getInstitutionCode();
        String oldBerthCode = checkin.getBerthCode();
        String oldBerthNumber = checkin.getBerthNumber();

        /*
         *修改入住状态为退住
         */
        checkin.setEndTime(new Date());
        //checkin.setMoneyPlan(moneyPlan);
        checkin.setStatus(2);
        checkinMapper.updateByCheckinCode(checkin);
        /*
         *修改老人余额
         */
        //elderlyService.updateByJianBalance(elderlyCode, moneyPlan);
        /*
         *添加结算记录
         */
        retreatSettleService.insert(retreatSettle);

        /*
         *重新插入入住记录
         */

        Date beTime = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String beginTime = sdf.format(beTime);


        Checkin checkin1 = new Checkin();
        if (checkin.getChargeType() == 1) {
            CheckinSetmeal checkinSetmeal = checkinSetmealService.getByCheckinSetmealCode(checkinSetmealCode);
            checkin1.setCheckinSetmealCode(checkinSetmealCode);
            checkin1.setCheckinSetmealName(checkinSetmeal.getName());
            price = checkinSetmeal.getUnivalence();
        }


        //计算居住天数
        int durationPlan = Util.caculateTotalTime(beginTime, endTimePlan);
        //计算预计费用
        double newMoneyPlan = DoubleUtil.mul(price, (double) durationPlan);

        Date beginTime1 = null;
        Date endTime1 = null;
        try {
            beginTime1 = sdf.parse(beginTime);
            endTime1 = sdf.parse(endTimePlan);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //修改老床位状态为0
        Berth berth = berthService.getByBerthCode(oldBerthCode);
        berth.setStatus(0);
        berthService.updateByBerthCode(berth);
        //修改新床位状态为1
        Berth berth1 = berthService.getByBerthCode(newBerthCode);
        berth1.setStatus(1);
        berthService.updateByBerthCode(berth1);


        checkin1.setCheckinCode(UUID.randomUUID().toString());
        checkin1.setBerthCode(newBerthCode);
        checkin1.setBerthNumber(berth1.getNumber());
        checkin1.setElderlyCode(elderlyCode);
        checkin1.setElderlyName(elderlyName);
        checkin1.setDurationPlan(durationPlan);
        checkin1.setMoneyPlan(newMoneyPlan);
        checkin1.setBeginTimePlan(beginTime1);
        checkin1.setBeginTime(beTime);
        checkin1.setEndTimePlan(endTime1);
        checkin1.setPrice(price);
        checkin1.setChargeType(checkin.getChargeType());
        checkin1.setCreateTime(new Date());
        checkin1.setType(checkin.getType());
        checkin1.setStatus(1);
        checkin1.setStaffCode(staffCode);
        checkin1.setInstitutionCode(institutionCode);
        checkinMapper.insert(checkin1);
//
//        /*
//         *转房记录插入
//         */
//        RoomChange roomChange = new RoomChange();
//        roomChange.setRoomChangeCode(UUID.randomUUID().toString());
//        roomChange.setElderlyCode(elderlyCode);
//        roomChange.setElderlyName(elderlyName);
//        roomChange.setOldBerthCode(oldBerthCode);
//        roomChange.setOldBerthNumber(oldBerthNumber);
//        roomChange.setNewBerthCode(newBerthCode);
//        roomChange.setNewBerthNumber(berth1.getNumber());
//        roomChange.setRemarks(remarks);
//        roomChange.setCreateTime(new Date());
//        roomChange.setStaffCode(staffCode);
//        roomChange.setInstitutionCode(institutionCode);
//        return roomChangeService.insert(roomChange);

        return 1;
    }



    /**
     * 添加入住资料
     *
     * @param checkin
     * @param berth
     * @param feeArr
     * @return
     */
    @Override
    @Transactional
    public int insertAndUpdateBerthStatus(Checkin checkin, Berth berth, JSONArray feeArr) {

        //更新床位状态
        berthService.updateByBerthCode(berth);

        return checkinMapper.insert(checkin);

    }


    @Override
    @Transactional
    public int updateByCheckinCode(Checkin checkin, Berth berth, JSONArray feeArr) {

        //判断修改的床位是否和之前一样，一样则不修改，不一样则修改状态
        if (!checkin.getBerthCode().equals(berth.getBerthCode())) {
            Berth berth1 = berthService.getByBerthCode(checkin.getBerthCode());
            berth1.setStatus(0);
            berthService.updateByBerthCode(berth1);

            berth.setStatus(1);
            berthService.updateByBerthCode(berth);

            //修改为新的床位
            checkin.setBerthCode(berth.getBerthCode());
            checkin.setBerthNumber(berth.getNumber());
        }

        return checkinMapper.updateByCheckinCode(checkin);
    }


    @Override
    public List<Checkin> getByCheckinSetmealCodeAndStatus1(String checkinSetmealCode) {
        return checkinMapper.getByCheckinSetmealCodeAndStatus1(checkinSetmealCode);
    }

    @Override
    public List<Checkin> getListByElderlyCode(String elderlyCode) {
        return checkinMapper.getListByElderlyCode(elderlyCode);
    }

    /**
     * 入住
     *
     * @param checkin
     * @param berth
     * @return
     */
    @Override
    @Transactional
    public int updateCheckinAndBerth(Checkin checkin, Berth berth) {
        berthService.updateByBerthCode(berth);
        return checkinMapper.updateByCheckinCode(checkin);
    }


    @Override
    public Checkin getByBerthCode(String berthCode,int status) {
        return checkinMapper.getByBerthCode(berthCode,status);
    }

}
